<?php

class Admini_ManagepayController extends Uuch_ManageController {

    public function init() {

        parent::init();
        $this->appendCss('/css/Share.css'); //    
        $this->appendCss('/css/home/warehouse.css');
        $this->appendCss('/admins/css/common.css');
        //$this->_helper->layout->setLayout('admin');
        $this->appendScript('/admins/js/jquery/jquery-1.7.2.min.js')->appendScript('/js/jquery.validate.min.js')->appendScript('/js/messages_cn.js')->appendScript('/js/uuch.js');
        $this->view->type = 'pay';
        $this->view->countagentpay = Application_Model_AgentPay::getcountagentpay();
        $this->view->countagentpay1 = Application_Model_AgentPay::getcountagentpay(Application_Model_AgentPay::STATUS_nopay);
        $this->view->countagentpay2 = Application_Model_AgentPay::getcountagentpay(Application_Model_AgentPay::STATUS_acpay);
        $this->view->countagentpay3 = Application_Model_AgentPay::getcountagentpay(Application_Model_AgentPay::STATUS_prpay);
        $this->view->countagentpay4 = Application_Model_AgentPay::getcountagentpay(Application_Model_AgentPay::STATUS_Refund);
        $this->view->countcharge = Application_Model_topUpAlipay::getcountchargr();
        $this->view->countcharge1 = Application_Model_topUpAlipay::getcountchargr(Application_Model_topUpAlipay::STATUS_WaitPay);
        $this->view->countcharge2 = Application_Model_topUpAlipay::getcountchargr(Application_Model_topUpAlipay::STATUS_Processing);
        $this->view->countcharge3 = Application_Model_topUpAlipay::getcountchargr(Application_Model_topUpAlipay::STATUS_Sended);
        $this->view->countcharge4 = Application_Model_topUpAlipay::getcountchargr(Application_Model_topUpAlipay::STATUS_Refund);
    }

    public function indexAction() {
        
    }

    /**
     * 代支付款管理
     */
    public function agentpayAction() {
        $this->setTitle("代支付管理");
        $count = 15;
        if ($this->_getParam('mg') == 2) {
            $this->view->message = "处理成功！";
        }

        $start = $this->_getParam('start');
        $quick = $this->_getParam('quick');
        if (!$start) {
            $start = 0;
        }
        $status = $this->_getParam('status');
        $stype = 1;
        if ($status != '') {
            if ($status == Application_Model_AgentPay::STATUS_nopay) {
                $stype = 2;
            } else if ($status == Application_Model_AgentPay::STATUS_acpay) {
                $stype = 3;
            } else if ($status == Application_Model_AgentPay::STATUS_prpay) {
                $stype = 4;
            }
        }
        $agentPaySearch = new Application_Model_Filter_AgentPaySearch();
        $agentPaySearch->quick = $quick;
        if ($status !== "") {
            $agentPaySearch->status = $status;
        }
        $agentPayItem = Application_Model_AgentPay::getByFilter2($agentPaySearch, $count, $start);
        $this->view->agentpayAll = $agentPayItem;
        $this->view->AgentpayList = $agentPayItem['list'];
        $this->view->stype = $stype;
        $this->view->quick = $quick;
        $this->view->type = 'agentpay';
    }

    /**
     * 代充值管理
     */
    public function chargeAction() {
        $this->setTitle("代充值管理");
        $count = 10;
        $start = $this->_getParam('start');

        $username = $this->_getParam('username');

        if (!$start) {
            $start = 0;
        }
        $status = $this->_getParam('status');
        $day = $this->_getParam('day');
        $stype = 1;
        if ($status != '') {
            if ($status == Application_Model_topUpAlipay::STATUS_WaitPay) {
                $stype = 2;
            } else if ($status == Application_Model_topUpAlipay::STATUS_Processing) {
                $stype = 3;
            } else if ($status == Application_Model_topUpAlipay::STATUS_Sended) {
                $stype = 4;
            } else if ($status == Application_Model_topUpAlipay::STATUS_Refund) {
                $stype = 5;
            }
        }
        $this->view->topUpAlipay = Application_Model_topUpAlipay::getByFilter($username, $status, $day, $count, $start);
        $this->view->topUpAlipayList = $this->view->topUpAlipay['list'];
        $this->view->stype = $stype;
        $this->view->username = $username;
        $this->view->type = 'pay';
    }

    /**
     * 代充值管理->支付处理
     */
    public function chargeaccepAction() {
        $idlist = $this->_getParam('idList');
        if ($idlist) {
            $ids = explode(",", $idlist);

            foreach ($ids as $id) {
                $item = Application_Model_topUpAlipay::load($id);

                if ($item->status == 1) {
                    $item->status = 10;
                    $item->dateProcee = date('Y-m-d H:i:s');
                    $item->save();
                    Application_Model_Operate::addOperate(($item->amount + $item->serviceFee), null, $item->id, "处理代充值", $item->userId);
                    echo json_encode(true);
                }
            }
        }
        exit;
    }

    public function payagentpayAction() {
        $idlist = $this->_getParam('idlist');
        $agentPay = new Application_Model_AgentPay();
        $result = $agentPay->proceeAgentPay($idlist);
        $this->_redirect('/managepay/agentpay/mg/2');
    }

    public function proceepayagentAction() {
        $id = $this->_getParam('id');
        $money = $this->_getParam("money");
        $agentPay = new Application_Model_AgentPay();

        $result = $agentPay->proceeAgentPay($id, $money);
        $this->json(array('data' => 1));
        $agent = Application_Model_AgentPay::load($id);

        $account = Application_Model_UserAccount::load($agent->memberId);
        
        $params = array(
            'account' => $account,
            'businessType' => 'spjz',
            'remark' => $agent->remark
        );
        $params['trader'] = 'backend-account-cr';
        if($agent->type == Application_Model_AgentPay::TYPE_Ingstore){
            $params['attribute'] = json_encode(array('express_company'=>$agent->expressCompany));
        }

        $this->_addFinance($agent, $money, $params);
        $params['trader'] = 'yq-uuchcn-dr';
        $this->_addFinance($agent, $money, $params);
        $params['trader'] = 'yq-uuchcn-cr';
        $this->_addFinance($agent, $money, $params);
        if($agent->type == Application_Model_AgentPay::TYPE_Ingstore){
            $params['trader'] = 'ec-account-dr';
        }else if($agent->type == Application_Model_AgentPay::TYPE_Taobao){
            $params['trader'] = 'seller-number-dr';
        }
        $this->_addFinance($agent, $money, $params);
    }

    /**
     * 代充值详情
     */
    public function chargedetailAction() {
        $this->setTitle("代充值详情");
        $id = $this->_getParam('id');
        if ($this->_getParam("operate") == "subcharge") {
            $chargeid = $this->_getParam('chargeid');
            $title = $this->_getParam('issuetitle');
            $content = $this->_getParam("issuecontent");
            $issuetype = $this->_getParam('issuetype');
            $userid = $this->_getParam('userid');
            $message = $this->_getParam('message');
            $mail = $this->_getParam('mail');
//                echo $chargeid."<br>".$title."<br>".$content."<br>".$issuetype."<br>".$userid."<br>".$message."<br>".$mail;exit;
            $reissue = Application_Model_problem::byaddissue($chargeid, $title, $content, $issuetype, $userid, $message, $mail, Application_Model_problem::TYPE_CHARGE);
            if (!$reissue) {
                $this->view->ma = 2;
            } else {
                $this->view->ma = 1;
            }
            $id = $chargeid;
        }
        $chargeItem = Application_Model_topUpAlipay::load($id);

        $this->view->chargeItem = $chargeItem;
        $this->view->issuedetail = Application_Model_problem::byOneissue($id, Application_Model_problem::TYPE_CHARGE);
    }

    /**
     * 代支付详情
     */
    public function agentpaydetailAction() {
        $this->setTitle("代支付详情");
        $id = $this->_getParam('id');

        
        if ($this->_getParam("operate") == "subagentpay") {
            $agentpayd = $this->_getParam('agentpayid');
            $title = $this->_getParam('issuetitle');
            $content = $this->_getParam("issuecontent");
            $issuetype = $this->_getParam('issuetype');
            $userid = $this->_getParam('userid');
            $message = $this->_getParam('message');
            $account = Application_Model_UserAccount::load($userid);
            $beforeMoney = $account->Money;
            $mail = $this->_getParam('mail');
//                echo $agentpayd."<br>".$title."<br>".$content."<br>".$issuetype."<br>".$userid."<br>".$message."<br>".$mail;exit;
            $reissue = Application_Model_problem::byaddissue($agentpayd, $title, $content, $issuetype, $userid, $message, $mail, Application_Model_problem::TYPE_AGENTPAY);
            if (!$reissue) {
                $this->view->ma = 2;
            } else {
                $this->view->ma = 1;
            }
            $id = $agentpayd;
            $agent = Application_Model_AgentPay::load($id);
            $afterMoney = $beforeMoney + $agent->payMoney;
            $params = array(
                'account' => $account,
                'businessType' => 'dzfspjztk',
                'beforeMoney' => $beforeMoney,
                'afterMoney' => $beforeMoney,
                'trader' => 'backend-account-cr',
                'remark' => $content
            );
            $params['attribute'] = json_encode(array('question_type'=>$issuetype));
            $this->_addFinance($agent, $agent->payMoney, $params);
            $params['trader'] = 'account-number-dr';
            $params['afterMoney'] = $afterMoney;
            
            $this->_addFinance($agent, $agent->payMoney, $params);
            $params['beforeMoney'] = $afterMoney;
            $params['afterMoney'] = $afterMoney;
            $params['businessType'] = 'fuwufeitk';
            $params['trader'] = 'backend-account-cr';
            $this->_addFinance($agent, $agent->processFee, $params);
            $params['trader'] = 'account-number-dr';
            $params['afterMoney'] = $afterMoney + $agent->processFee;
            $this->_addFinance($agent, $agent->processFee, $params);
        }
        $agentpayItem = Application_Model_AgentPay::load($id);

        $agentPay=Application_Model_PaypalRisk::getAgentPay($agentpayItem->memberId);
        $this->view->agentPays=$agentPay;
        
        Application_Model_Operate::addOperate(($agentpayItem->payMoney + $agentpayItem->processFee), null, $id, "查看代充值详情", $agentpayItem->memberId);
        $this->view->agentpayItem = $agentpayItem;
        $this->view->issuedetail = Application_Model_problem::byOneissue($id, Application_Model_problem::TYPE_AGENTPAY);
        $this->view->type = 'agentpay';
    }

    protected function _addFinance($agent, $money, $params) {


        require_once(APPLICATION_PATH . '/../library/finance/General.php');
        $account = $params['account'];
        $beforeMoney = isset($params['beforeMoney']) ? $params['beforeMoney'] : $account->Money;
        $afterMoney = isset($params['afterMoney']) ? $params['afterMoney'] : $account->Money;
        $trader = isset($params['trader']) ? $params['trader'] : '';
        $user = Application_Model_User::load($agent->memberId);
        if($trader == 'backend-account-cr'){
            $beforeMoney = $afterMoney;
        }
        $remark = isset($params['remark']) ? $params['remark'] : '';
        $data = array(
            'transaction_number' => '',
            'trader' => $trader,
            'customer_id' => $agent->memberId,
            'customer_name' => $user->nikename,
            'member' => 'admin',
            'amount' => $money,
            'currency' => 'CNY',
            'rate' => '1',
            'rate_amount' => '0',
            'period_start' => $beforeMoney,
            'period_end' => $afterMoney,
            'business_type' => $params['businessType'],
            'order_sn' => '',
            'remark' => $remark,
            'created_at' => date('Y-m-d H:i:s')
        );
        if(isset($params['attribute'])){
            $data['attribute'] = $params['attribute'];
        }

        try {
            $api = new General($beforeMoney, $params['account']);
            $api->setFields($data);
            $api->addTransaction();
            $result = array('code' => 'success', 'message' => '');
        } catch (Exception $ex) {
            echo $ex->getMessage();
            $result = array('code' => 'error', 'message' => $ex->getMessage());
        }
    }

}
